<?php

namespace Maestros\Controller\Service;

use Zend\Db\Adapter\Adapter;
use Componentes\Controller\UtilsArrayController;
use Maestros\Model\Entity\LapsoAcademico;
use Maestros\Model\Dao\LapsoAcademicoDAO;
use Maestros\Controller\Service\GenericService;

class LapsoAcademicoService extends GenericService {
    /* /**
     * @var Zend\Db\Adapter\Adapter 
     */

    private $dbAdapter;
    private static $lapsoAcademicoService;

    /**
     * @param \Zend\Db\Adapter\Adapter $dbAdapter
     */
    private function __construct(Adapter $dbAdapter) {
        $this->dbAdapter = $dbAdapter;
    }

    public static function getInstance($adapter) {
        if (self::$lapsoAcademicoService == null) {
            self::$lapsoAcademicoService = new LapsoAcademicoService($adapter);
        }
        return self::$lapsoAcademicoService;
    }

    public function getLapsoAcademicoDao() {
        return LapsoAcademicoDAO::getInstance($this->dbAdapter);
    }

    public function getRegistro($id) {
        $data = null;
        $success = false;
        $mensaje = 'LAPSO NO ENCONTRADO';
        $lapso = $this->obtenerLapsoSegunId($id);
        if ($lapso->getId() != null) {
            $data[0] = $lapso->objectToArray();
            $mensaje = 'LAPSO ENCONTRADO';
            $success = true;
        }
        $output = array(
            'success' => $success,
            'data' => $data,
            'total' => count($data),
            'mensaje' => $mensaje
        );
        return $output;
        //return UtilsArrayController::organizarOutput($success, $data, null);
    }

    public function obtenerLapsoSegunId($id) {
        $registros = $this->getLapsoAcademicoDao()->getRegistro($id);
        $data = UtilsArrayController::llenarData($registros);
        $lapso = new LapsoAcademico($data[0]);
        return $lapso;
    }

    /**
     * Retorna un registro completo dado un id de malla de la tabla malla
     * @param $id type string
     * @return Objeto malla
     */
    public function obtenerLapsoAcademicoSegunIdLapso($idLapso) {
        $registros = $this->getLapsoAcademicoDao()->getLapsoAcademicoSegunIdLapso($idLapso);
        $data = UtilsArrayController::llenarData($registros);
        $lapsoAcademico = new LapsoAcademico($data[0]);
        $lapsoAcademico->setLapso($data[0]['id_lapso']);
        $lapsoAcademico->setMetodologiaEstudio($data[0]['id_metodologia_estudio']);
//        echo 'hhhhhh>';
//        echo $lapsoAcademico->getLapso();
//        echo '<hhhhhh';
        return $lapsoAcademico;
    }

    //put your code here
}
